/**
 * SeriesController
 * @Chunning.Xu
 * 2011 Nov 22, 2011 6:39:24 PM
 **/
package com.lux.product.controllers
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	import com.lux.product.controllers.queue.ActionQueue;
	import com.lux.product.controllers.queue.QueueEvents;
	import com.lux.product.controllers.queue.QueueItem;
	import com.lux.product.models.ProductModel;
	import com.lux.product.models.SeriesModel;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	[Event(name="complete", type="flash.events.Event")]
	
	public class SeriesController extends LuxViewController
	{
		//START.
		
		private const LIST_MAX_WIDTH:Number = 122;
		private const LIST_MAX_HEIGHT:Number = 266;
		private const SPACE:Number = 15;
		
		private const SINGLE_MAX_WIDTH:Number = 160.0;
		private const SINGLE_MAX_HEIGHT:Number = 298;
		
		private var ctrlList:Vector.<ProductController>;
		private var _series:SeriesModel;
		
		private var originRect:Rectangle;
		private var selectedCtrl:ProductController;
		private var queue:ActionQueue;
		
		
		public function SeriesController(series:SeriesModel)
		{
			super(new Sprite);
			
			this._series = series;
			initialize();
		}
		
		private function setItemSize(item:ProductController):void
		{
			var rate:Number = item.opacRect.width > item.opacRect.height ? LIST_MAX_WIDTH / item.opacRect.width :
																		   LIST_MAX_HEIGHT / item.opacRect.height;
			rate = Math.min(1,rate);
			
			item.view.scaleX = rate;
			item.view.scaleY = rate;
		}
		
		private function initialize():void
		{
			queue = new ActionQueue;
			queue.addEventListener(QueueEvents.PROCESS_COMPLETE,function(evt:Event):void{dispatchEvent(new Event(Event.COMPLETE))});
			
			ctrlList = new Vector.<ProductController>;
			var width:Number = 0;
			var maxHeight:Number = 0;
			for each(var pModel:ProductModel in series.products)
			{
				var pCtrl:ProductController = new ProductController(pModel);
				setItemSize(pCtrl);

				width += pCtrl.opacRect.width * pCtrl.view.scaleX + SPACE;
				maxHeight = Math.max(maxHeight,pCtrl.opacRect.height * pCtrl.view.scaleY);

				ctrlList.push(pCtrl);
			}
			width -= SPACE;
			
			var startX:Number = 0 - width / 2;
			var startY:Number = maxHeight / 2;

			for(var i:int = 0;i < ctrlList.length;i++)
			{
				var prd:ProductController = ctrlList[i];
				prd.view.x = startX;
				prd.view.y = startY - prd.opacRect.height * prd.view.scaleY;
				
				startX += SPACE + prd.opacRect.width * prd.view.scaleX;
				view.addChild(prd.view);
			}
			 
		}

		public function get series():SeriesModel
		{
			
			return _series;
		}
		
		private function easeOut(item:ProductController):void
		{
			for each(var pCtrl:ProductController in ctrlList)
			{
				if(pCtrl == item)
					continue;
				TweenLite.to(pCtrl.view,0.5,{alpha:1.0,ease:Cubic.easeInOut});
			}
			
			TweenLite.to(item.view,0.5,{x:originRect.x,y:originRect.y,scaleX:originRect.width,scaleY:originRect.height,onComplete:onEaseOutComplete,ease:Cubic.easeInOut});
		}
		
		private function onEaseOutComplete():void
		{
			for each(var pCtrl:ProductController in ctrlList)
				pCtrl.enabled = true;
			
			queue.processNext();
		}
		
		private function easeIn(item:ProductController):void
		{
			var rate:Number = item.opacRect.width > item.opacRect.height ? SINGLE_MAX_WIDTH / item.opacRect.width :
																		   SINGLE_MAX_HEIGHT / item.opacRect.height;
			rate = Math.min(1,rate);
			var center:Point = new Point(item.opacRect.x + item.opacRect.width / 2,item.opacRect.y + item.opacRect.height / 2);
			var matrix:Matrix = new Matrix(rate,0,0,rate,0 - rate * center.x,0 - rate * center.y);

			TweenLite.to(item.view,1.0,{scaleX:rate,scaleY:rate,x:matrix.tx,y:matrix.ty,onComplete:queue.processNext,ease:Cubic.easeInOut});
			
			for each(var pCtrl:ProductController in ctrlList)
			{
				pCtrl.enabled = false;
				if(pCtrl == item)
					continue;
				TweenLite.to(pCtrl.view,0.5,{alpha:0,ease:Cubic.easeInOut});
			}
		}
		
		private function switchAnimated():void
		{
			if(index < 0)
			{
				queue.push(new QueueItem(easeOut,[selectedCtrl]));
				selectedCtrl = null;
			}else{
				if(selectedCtrl != null)
					queue.push(new QueueItem(easeOut,[selectedCtrl]));
				selectedCtrl = ctrlList[index];
				originRect = new Rectangle(selectedCtrl.view.x,selectedCtrl.view.y,selectedCtrl.view.scaleX,selectedCtrl.view.scaleY);
				queue.push(new QueueItem(easeIn,[selectedCtrl]));
			}
		}
		
		private function switchNoAnimated():void
		{
			var selectedItem:ProductController = index >= 0 ?  ctrlList[index] : null;
			for each(var item:ProductController in ctrlList)
			{
				if(selectedItem == item)
					continue;
				item.view.alpha = index < 0 ? 1 : 0;
				item.enabled = index < 0;
			}
			if(index < 0)
			{
				selectedCtrl.enabled = true;
				selectedCtrl.view.x = originRect.x;
				selectedCtrl.view.y = originRect.y;
				selectedCtrl.view.scaleX = originRect.width;
				selectedCtrl.view.scaleY = originRect.height;
				selectedCtrl = null;
			}else{
				if(selectedCtrl != null)
				{
					selectedCtrl.view.x = originRect.x;
					selectedCtrl.view.y = originRect.y;
					selectedCtrl.view.scaleX = originRect.width;
					selectedCtrl.view.scaleY = originRect.height;
				}
				
				selectedCtrl = selectedItem;
				originRect = new Rectangle(selectedCtrl.view.x,selectedCtrl.view.y,selectedCtrl.view.scaleX,selectedCtrl.view.scaleY);
				
				var rate:Number = selectedItem.opacRect.width > selectedItem.opacRect.height ? SINGLE_MAX_WIDTH / selectedItem.opacRect.width :
					SINGLE_MAX_HEIGHT / selectedItem.opacRect.height;
				rate = Math.min(1,rate);
				var center:Point = new Point(selectedItem.opacRect.x + selectedItem.opacRect.width / 2,selectedItem.opacRect.y + selectedItem.opacRect.height / 2);
				var matrix:Matrix = new Matrix(rate,0,0,rate,0 - rate * center.x,0 - rate * center.y);
				selectedItem.enabled = false;
				selectedItem.view.alpha = 1;
				selectedItem.view.x =  matrix.tx;
				selectedItem.view.y = matrix.ty;
				selectedItem.view.scaleX = rate;
				selectedItem.view.scaleY = rate;
			}
		}
		
		public function setIndex(index:int,animated:Boolean = false):void
		{
			if(index == this._index || index >= ctrlList.length)
				return;
			
			_index = index;

			if(animated)
				switchAnimated();
			else
				switchNoAnimated();
		}
		
		private var _index:int = -1;
		public function set index(index:int):void
		{
			if(index == this._index || index >= ctrlList.length)
				return;
			
			_index = index;
			
			if(index < 0)
			{
				queue.push(new QueueItem(easeOut,[selectedCtrl]));
				selectedCtrl = null;
			}else{
				if(selectedCtrl != null)
					queue.push(new QueueItem(easeOut,[selectedCtrl]));
				selectedCtrl = ctrlList[index];
				originRect = new Rectangle(selectedCtrl.view.x,selectedCtrl.view.y,selectedCtrl.view.scaleX,selectedCtrl.view.scaleY);
				queue.push(new QueueItem(easeIn,[selectedCtrl]));
			}
		}
		
		public function get index():int
		{
			return _index;
		}
		
		public function get idle():Boolean
		{
			return queue.idle;
		}
		
		//END
	}
}